package com.yqg.protection.controller;

import com.alibaba.fastjson.JSON;
import com.yqg.protection.controller.dataHandler.DetectDataHandler;
import com.yqg.protection.controller.dataHandler.FireCockDataHandler;
import com.yqg.protection.controller.vo.FireCockVo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/aiot")
public class AIoTController {

    @Resource
    FireCockDataHandler fireCockDataHandler;

    @Resource
    DetectDataHandler detectDataHandler;

    @PostMapping("/receive")
    public ResponseEntity<String> handleMessage(
            @RequestBody FireCockVo fireCockVo  // 自动反序列化JSON
    ) {
        // 1. 打印接收到的消息
        System.out.println("收到设备消息: " + JSON.toJSONString(fireCockVo));
        // 2. 保存数据
        fireCockDataHandler.saveFireCockData(fireCockVo);
        // 3. 返回成功响应
        return ResponseEntity.ok("消息接收成功");
    }

    @PostMapping("/detectDataReceive")
    public ResponseEntity detectDataReceive(
            @RequestBody Map<String, Object> detectData  // 自动反序列化JSON
    ) {
        detectDataHandler.saveDetectData(detectData);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("code", "ok");
        return ResponseEntity.ok(hashMap);
    }
}
